package org.javaboy.vhr.mapper;

import org.apache.ibatis.annotations.*;
import org.javaboy.vhr.model.*;

import java.util.Date;
import java.util.List;

/**
 * ClassName: ApplyInsMapper <br/>
 * Description: <br/>
 * date: 2020/3/2 20:21<br/>
 *
 * @author Hesion<br />
 * @since JDK 1.8
 */
@Mapper
public interface BorrowCarMapper {
    @Select("SELECT * FROM hr")
    List<Hr> getList();

    @Insert("INSERT INTO borrow_car VALUES(#{id},#{cId},#{uId},#{duration},#{mileage},#{spend},#{createTime},#{createBy},#{updateTime},#{updateBy},#{del_flag})")
    int apply(BorrowCar borrowCar);

    @Update("UPDATE car SET borrow=1 WHERE id=#{cId}")
    int updateBorrow(BorrowCar borrowCar);

    @Select({"<script> " +
            "SELECT * FROM v_borrow_car " +
            "<if test=\"borrowBean.model != null and borrowBean.model != ''\">" +
            "AND model like concat('%',#{borrowBean.model},'%')</if>" +
            "<if test=\"page != null and size != null\">" +
            "limit #{page},#{size}</if>" +
            "</script>"})
    @Results(id = "BorrowinfoResult",
            value = {
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updateTime", column = "update_time"),
                    @Result(property = "createBy", column = "create_by"),
                    @Result(property = "updateBy", column = "update_by"),
            })
    List<BorrowBean> getBorrowByPage(Integer page, Integer size, BorrowBean borrowBean, Date[] beginDateScope);

    @Select({"<script> " +
            "SELECT count(*) FROM v_borrow_car" +
            "<if test=\"borrowBean.model != null and borrowBean.model != ''\">" +
            "and model=#{borrowBean.model}</if>" +
            "</script>"})
    Long getTotal(BorrowBean borrowBean, Date[] beginDateScope);

    @Delete("DELETE FROM borrow_car WHERE cId = #{id}")
    int backCar(String id);

    @Update("UPDATE car SET borrow=0 WHERE id=#{cId}")
    int updateBack(String id);

    @Update("UPDATE car SET mileage=mileage+#{mileage},spend=spend+#{spend} WHERE id=#{id}")
    int updateData(BorrowBean borrowBean);

    @Update("UPDATE borrow_car SET mileage=#{mileage},spend=#{spend},update_by=#{updateBy},update_time =#{updateTime} WHERE cId=#{id}")
    int changeMileAndSpend(BorrowCar borrowCar);
}
